package com.example.perplonews.a_a_mvp.model.models

import BaseModel
import com.example.perplonews.a_a_mvp.contract.Login_Contract
import com.example.perplonews.a_a_mvp.model.HttpCode
import com.example.perplonews.a_b_entity.BaseEntity
import com.example.perplonews.a_b_entity.ResponseUserEntity
import com.example.perplonews.a_c_network.Api
import com.example.perplonews.a_c_network.HttpFactory
import com.example.perplonews.a_c_network.HttpType
import io.reactivex.Observable
import io.reactivex.functions.Function

class   Login_Model : BaseModel(), Login_Contract.LoginModel {
    override fun request(map: HashMap<String, Any>): Observable<BaseEntity>? {
        var ob: Observable<BaseEntity>? = null

        var obs: ArrayList<Observable<BaseEntity>> = ArrayList()
        var code = map["code"] as Int
        map.remove("code")
        if (code == HttpCode.LOGINCODE) {
            var body = createBody(map)
            var requestLogin :Observable<ResponseUserEntity?>?=
                HttpFactory.instance.factory(HttpType.TOKENSIGNTYPE)
                    ?.getRetrofit()
                    ?.create(Api::class.java)
                    ?.requestLogin(body)
            var map1 = requestLogin?.map(object : Function<ResponseUserEntity?, BaseEntity> {
                override fun apply(t: ResponseUserEntity): BaseEntity {
                    return t
                }

            })
           obs.add(map1!!)

        } else if (code == HttpCode.REGISTCODE) {
            var body = createBody(map)
            var requestRegister: Observable<ResponseUserEntity?>? =
                HttpFactory.instance.factory(HttpType.TOKENSIGNTYPE)
                    ?.getRetrofit()
                    ?.create(Api::class.java)
                    ?.requestRegister(body)
            var map1 = requestRegister?.map(object : Function<ResponseUserEntity?, BaseEntity> {
                override fun apply(t: ResponseUserEntity): BaseEntity {
                    return t
                }

            })
            obs.add(map1!!)


        }

        ob = Observable.merge(obs)
        return ob


//区分当前方法执行哪个接口的请求
        //区分当前方法执行哪个接口的请求
//        val code = map["code"] as Int
//        map.remove("code")
//        if (code == HttpCode.LOGINCODE) {
//            //当前为登录接口逻辑使用
//            //map->json字符串->生成requestbody
//            val body = createBody(map)!!
//            //根据在api中配置对应的服务器json返回值生成的bean类
//            val ob: Observable<ResponseUserEntity?>? =
//                HttpFactory.instance.factory(HttpType.TOKENSIGNTYPE)
//                    ?.getRetrofit()
//                    ?.create(Api::class.java)
//                    ?.requestLogin(body)
//            //对当前的异步任务进行转化
//            return ob?.map(object : Function<ResponseUserEntity?, BaseEntity> {
//                override fun apply(t: ResponseUserEntity): BaseEntity {
//                    return t
//                }
//            })
//        } else if (code == HttpCode.REGISTCODE) {
//            //当前为注册接口逻辑使用
//            val body = createBody(map)!!
//            val ob: Observable<ResponseUserEntity?>? =
//                HttpFactory.instance.factory(HttpType.TOKENSIGNTYPE)
//                    ?.getRetrofit()
//                    ?.create(Api::class.java)
//                    ?.requestRegister(body)
//            return ob?.map(object : Function<ResponseUserEntity?, BaseEntity> {
//                override fun apply(t: ResponseUserEntity): BaseEntity {
//                    return t
//                }
//
//            })
//        }
//        return null
    }


}